package com.woniuxy.service.location.impl;

import com.woniuxy.entity.City;
import com.woniuxy.entity.County;
import com.woniuxy.entity.Hospital;
import com.woniuxy.entity.Province;
import com.woniuxy.mapper.CityMapper;
import com.woniuxy.mapper.CountyMapper;
import com.woniuxy.mapper.HospitalMapper;
import com.woniuxy.mapper.ProvinceMapper;
import com.woniuxy.service.location.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 马踏东京赏樱花
 */
@Service
public class ProvinceServiceImpl implements ProvinceService {
    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private CountyMapper countyMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private HospitalMapper hospitalMapper;

    @Override
    public List<Province> findAllProvince() {
        List<Province> provinces = provinceMapper.selectByExample(null);
        return provinces;
    }

    @Override
    public int addOne(String str) {
        Province province = new Province();
        province.setProvinceName(str);
        int insert = provinceMapper.insert(province);
        if (insert == 1) {
            return insert;
        }
        throw new RuntimeException("添加失败");
    }

    @Override
    public int deleteOne(String str) {
        if (str == null) {
            throw new RuntimeException("数据获取失败");
        }
        Province province = new Province();
        province.setProvinceId(Integer.valueOf(str));
        return provinceMapper.deleteByPrimaryKey(province.getProvinceId());
    }

    @Override
    public Province findOne(String str) {
        if (str == null) {
            throw new RuntimeException("数据获取失败");
        }
        Province province = new Province();
        province.setProvinceId(Integer.valueOf(str));
        return provinceMapper.selectByPrimaryKey(province.getProvinceId());
    }


    @Override
    public List<Province> findAll() {
        List<Province> provinces = provinceMapper.selectByExample(null);
        List<City> cityList = cityMapper.selectByExample(null);
        List<County> counties = countyMapper.selectByExample(null);
        List<Hospital> hospitals = hospitalMapper.selectByExample(null);

        counties.stream().forEach(county -> {
            List<Hospital> hospitalList = new ArrayList<>();
            hospitals.stream()
                    .filter(hospital -> hospital.getCountyId()==county.getCountyId())
                    .forEach(hospital -> hospitalList.add(hospital));
            county.setHospitalList(hospitalList);
        });


        cityList.stream().forEach(city -> {
            List<County> countyList = new ArrayList<>();
            counties.stream().filter(county -> city.getCityId()==county.getCityId()).forEach(county -> countyList.add(county));
            city.setCountyList(countyList);
        });

        provinces.stream().forEach(province -> {
            List<City> cityList1 = new ArrayList<>();
            cityList.stream()
                    .filter(city -> city.getProvinceId()==province.getProvinceId())
                    .forEach(city -> cityList1.add(city));
            province.setCityList(cityList1);
        });
        return provinces;
    }

    @Override
    public Province updateOne(Province province) {
        int i = provinceMapper.updateByPrimaryKey(province);
        if (i > 0) {
            return province;
        }
        throw new RuntimeException("修改失败");
    }
}
